package com.atguigu.yygh.common.handler;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public R error(Exception ex){
        ex.printStackTrace();
        log.error("全局异常："+ex.getLocalizedMessage());
        return R.error();
    }

    @ExceptionHandler(value = YyghException.class)
    public R error(YyghException ex){
        ex.printStackTrace();
        log.error("YyghException："+ex.getLocalizedMessage());
        return R.error().code(ex.getCode()).message(ex.getMessage());
    }

}
